﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WWDemon.Core;
using WWDemon.Core.WebSocket;
using WWDemon.Log;
using WWDemon.Model;

namespace WWDemon.Client
{
    public class ClientServer
    {
        private static readonly object objLock = new object();
        private static ClientServer server;

        public static ClientServer Instance
        {
            get
            {
                if (server == null)
                    lock (objLock)
                    {
                        if (server == null) server = new ClientServer();
                    }
                return server;
            }
        }

        private WebSocketClient webSocket;


        public async Task Init(string url,int prot)
        {
            webSocket = new WebSocketClient(url,prot);
            webSocket.OnClose += this.WebSocket_OnClose;
            webSocket.OnConnect += WebSocket_OnConnect;
            webSocket.OnReceive += WebSocket_OnReceive;

            await webSocket.Connect();
        }

        private bool IsLogin = false;

        private Task WebSocket_OnReceive(Model.CommondPC receiveData)
        {
            //这里 PC客户端 收到服务器的命令 GetOrderInfo
            switch (receiveData.Key)
            {
                case Model.CommondKey.Login:
                    IsLogin = true;
                    //做自己登陆后的业务

                    break;
                case Model.CommondKey.GetOrderInfo:
                    if (!IsLogin) return Task.CompletedTask;
                    //做自己订单业务

                    //GetOrderInfo 命令

                    //客户端做自己的业务


                    //收到服务器 获取订单命令
                    var orderInfo = receiveData.Data.ToObjectByte<GetOrderInfo>();
                    //做自己业务


                    break;
            }


            //通知订阅者
            GlobalSubPub.Instance.Publish(receiveData.Key, receiveData);

            return Task.CompletedTask;
        }

        private Task WebSocket_OnConnect(bool state)
        {
            LogHelper.Log(LogDefine.LogDebug, "成功链接到服务器");
            GlobalSubPub.Instance.Publish(Model.CommondKey.Connect, "");

            return Task.CompletedTask;
        }

        private Task WebSocket_OnClose()
        {
            IsLogin = false;
            LogHelper.Log(LogDefine.LogWarn, "与服务器断开链接");

            GlobalSubPub.Instance.Publish( Model.CommondKey.Close, "");


            return Task.CompletedTask;
        }





        public void SendData(Enum mainCmd,  byte[] data)
        {
            if (webSocket == null) return;

            webSocket.SendData(mainCmd, data);
        }

        public void SendData<T>(Enum mainCmd, T data)
        {
            if (webSocket == null) return;
            webSocket.SendData(mainCmd, data);
        }

        public void SendData(Enum mainCmd)
        {
            if (webSocket == null) return ;

            webSocket.SendData(mainCmd);
        }
    }
}
